/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mxi.its.dds2.Bean;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import mx.its.dsw2.biblioteca.Autor;
import mx.its.dsw2.biblioteca.Categoria;
import mx.its.dsw2.biblioteca.Categoriadelibro;
import mx.its.dsw2.biblioteca.Libro;
import mx.its.dsw2.biblioteca.bl.AutorBlLocal;
import mx.its.dsw2.biblioteca.bl.CategoriaBlLocal;
import mx.its.dsw2.biblioteca.bl.LibrolLocal;
import mx.its.dsw2.biblioteca.dao.AutorDaoLocal;
import mx.its.dsw2.biblioteca.dao.CategoriadelibroFacadeLocal;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.ToggleEvent;
import org.primefaces.event.UnselectEvent;

/**
 *
 * @author Carlos Asis
 */
@ManagedBean
@SessionScoped
public class BookBean {

    @EJB
    private CategoriadelibroFacadeLocal categoriadelibroFacade;
    @EJB
    private CategoriaBlLocal categoriaBl;
    @EJB
    private AutorDaoLocal autorDao;
    @EJB
    private AutorBlLocal autorBl;
    @EJB
    private LibrolLocal librol;
    private List<Libro> list;
    private List<Autor> listAutor;
    private Libro book;
    private String idAuthor;
    private List<Libro> filteredBook;
    private List<Categoria> listCategorias;
    private List<Categoria> categoriasSelect;

    /**
     * Creates a new instance of BookBean
     */
    public BookBean() {
        book = new Libro();
    }

    //Metodos------------ get y set ----------------------
    public List<Categoria> getListCategorias() {
        listCategorias = categoriaBl.getListAll();
        return listCategorias;
    }

    public List<Categoria> getCategoriasSelect() {
        return categoriasSelect;
    }

    public void setCategoriasSelect(List<Categoria> categoriasSelect) {
        this.categoriasSelect = categoriasSelect;
    }

    public void setListCategorias(List<Categoria> listCategorias) {
        this.listCategorias = listCategorias;
    }

    public List<Libro> getFilteredBook() {
        return filteredBook;
    }

    public void setFilteredBook(List<Libro> filteredBook) {
        this.filteredBook = filteredBook;
    }

    public List<Autor> getListAutor() {
        listAutor = autorBl.getListAll();
        return listAutor;
    }

    public void setListAutor(List<Autor> listAutor) {
        this.listAutor = listAutor;
    }

    public String getIdAuthor() {
        return idAuthor;
    }

    public void setIdAuthor(String idAuthor) {
        this.idAuthor = idAuthor;
    }

    public List<Libro> getList() {
        list = librol.getListAll();
        return list;
    }

    public void setList(List<Libro> list) {
        this.list = list;
    }

    public Libro getBook() {
        return book;
    }

    public void setBook(Libro book) {
        this.book = book;
    }
    //------------------- Acciones -----------------------
    /////////////////////Para el registro libro saber cuando se abre//////////////

    public String crearBook() {
        book.setIdAutor(autorDao.find(Integer.parseInt(idAuthor)));
        librol.register(book, listCategorias);
        return "BookFacelets";
    }

    public void handleToggle(ToggleEvent event) {

        if (event.getVisibility().name().equals("VISIBLE")) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, event.getComponent().getId() + " toggled", "Status:" + event.getVisibility().name());
            addMessage(message);
            // this.reset();
            book = new Libro();
        } else if (event.getVisibility().name().equals("HIDDEN")) {
            this.reset();
            book = new Libro();
        } else {
        }
    }

    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);


    }

    public void reset() {
        RequestContext.getCurrentInstance().reset("formBook:panelRegistro");

    }

    /////////////termina 
    //////////////Editar datos de la tabla///////////////
    public void onEdit(SelectEvent event) {
        FacesMessage msg = new FacesMessage("Car Selected", book.getNombre());

        FacesContext.getCurrentInstance().addMessage(null, msg);
        idAuthor = book.getIdAutor().getId() + "";
        categoriasSelect=new ArrayList<Categoria>();
        categoriasSelect = categoriaBl.getListByBook(book);
//        List<Categoria> listCate = new ArrayList<Categoria>();
//        for (Categoriadelibro cLibro : categoriadelibroFacade.getByLibro(book)) {
//            System.out.println("hola" + cLibro.getIdCategoria().getId());
//            listCate.add(cLibro.getIdCategoria());
//           
//        }
//        categoriasSelect=listCate;
//        for (Categoria categoria : listCate) {
//            System.out.println(categoria);
//        }
//        System.out.println("book" + idAuthor);
    }

    public void onRowUnselect(UnselectEvent event) {
        FacesMessage msg = new FacesMessage("Car Unselected", ((Libro) event.getObject()).getNombre());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void handleSave() {
        book.setIdAutor(autorDao.find(Integer.parseInt(idAuthor)));
        librol.modify(book, categoriasSelect);
        
    }
//    public String editBook() {
//        librol.modify(book);
//        return "UsuarioFacelets";
//    }
    ////////////////////////terminba edicion
    //------------------- Listenrs -----------------------
}
